package jku.semtech.rdf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import jku.semtech.common.FieldPosition;
import jku.semtech.common.Player;
import jku.semtech.common.Team;
import jku.semtech.sparql.DBPediaCrawler;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

public class TeamRDF {
	private Model model;
	private ArrayList<Resource> resources;
	private Map<String, Team> teams;
	private String nsTeam;
	private String nsPlayer;
	private Property isPlayerOf;
	private DBPediaCrawler dbCrawler;
	private String[] playersTeam1 = new String[] { "Iker Casillas",
			"Lionel Messi", "Cristiano Ronaldo", "John Terry",
			"Xabi Alonso", "David Silva", "Robert Lewandowski", "Rio Ferdinand", "William Gallas" };
	private String[] playersTeam2 = new String[] { "Ashley Cole",
			"Ashley Cole", "Carles Puyol", "David Luiz",
			"Patrice Evra", "Andrea Pirlo", "Mario Balotelli", "David Villa", "Karim Benzema" };

	public TeamRDF() {
		this.model = ModelFactory.createDefaultModel();
		boolean isFirst = true;
		Team team1 = new Team("Team1", "T1", "Johann", "stadium1", 1000);
		for (String playerName : playersTeam1) {
			team1.addPlayer(new Player(playerName,
					isFirst ? FieldPosition.GOALKEEPER : FieldPosition
							.getRandom()));
			isFirst = false;
		}

		isFirst = true;
		Team team2 = new Team("Team2", "T2", "Elisabeth", "stadium2", 2000);
		for (String playerName : playersTeam2) {
			team2.addPlayer(new Player(playerName,
					isFirst ? FieldPosition.GOALKEEPER : FieldPosition
							.getRandom()));
			isFirst = false;
		}
		// TeamModel team3 = new TeamModel("Team3", "T3", "Bernhard",
		// "stadium3",
		// 3000);

		teams = new HashMap<String, Team>();

		teams.put(team1.getTeamName(), team1);
		teams.put(team2.getTeamName(), team2);

		createNameSpace(team1);
		createNameSpace(team2);

		System.out.println("######## TURTLE ###########");
		model.write(System.out, "TURTLE");

		System.out.println("\n######## RDF/XML ##########");
		model.write(System.out,"RDF/XML");

		setDbCrawler(new DBPediaCrawler(this));
	}

	/* ********************** */
	/* ******* Methods ****** */
	/* ********************** */
	public void createNameSpace(Team team) {
		nsTeam = "http://jku.semtech.rdf/" + replaceWhitespace(team.getTeamName() + "/");
		model.setNsPrefix(team.getNickName(), nsTeam);
		nsPlayer = "http://jku.semtech.rdf/Player.rdf#";
		model.setNsPrefix("Player", nsPlayer);
		setProperties();
		createRessource(team.getPlayers());
		makeGraph(team);
	}

	public void createRessource(Map<String, Player> players) {
		resources = new ArrayList<Resource>();
		for (Player player : players.values()) {
			resources.add(model.createResource(nsTeam + player.getDbpediaID()));
		}
	}

	private void makeGraph(Team team) {
		for (Resource resource : resources) {
			resource.addProperty(isPlayerOf, nsTeam);
		}
	}

	public static String replaceWhitespace(String name) {
		return name.replace(' ', '_');
	}

	public void setProperties() {
		isPlayerOf = model.createProperty(nsPlayer + "isPlayerOf");
	}

	/* ********************** */
	/* ***** Properties ***** */
	/* ********************** */
	public Map<String, Team> getTeams() {
		return teams;
	}

	public void setTeams(Map<String, Team> teams) {
		this.teams = teams;
	}

	public Model getModel() {
		return model;
	}

	public void setModel(Model model) {
		this.model = model;
	}

	public DBPediaCrawler getDbCrawler() {
		return dbCrawler;
	}

	public void setDbCrawler(DBPediaCrawler dbCrawler) {
		this.dbCrawler = dbCrawler;
	}

}
